#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3 + 10, INF = 0x3f3f3f3f;
int n, m;
int mp[N][N];
bool st[N];
int dis[N];
void dijkstra()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[1] = 0;
	for (int i = 0; i < n; i++) {
		int t = -1;
		for (int j = 1; j <= n; j++) {
			if (!st[j] && (t == -1 || dis[t] > dis[j])) {
				t = j;
			}
		}
		st[t] = 1;
		for (int j = 1; j <= n; j++) {
			dis[j] = min(dis[j], dis[t] + mp[t][j]);
		}
	}
}
int main()
{
	cin >> m >> n;
	memset(mp, 0x3f, sizeof(mp));
	while (m--) {
		int a, b, w;
		cin >> a >> b >> w;
		mp[a][b] = mp[b][a] = min(mp[a][b], w);
	}
	dijkstra();
	cout << dis[n] << endl;
	return 0;
}